﻿Imports Actuariat.comptes
Imports Actuariat

Public Class PTIC_vueBilan

    Protected _bilan As bilan_V2
    Protected _compteBesoinsFondsPropres As compteBesoinsFondsPropres_V2

#Region "Propriétés"
    Public Property compteBilan() As bilan_V2
        Get
            Return _bilan
        End Get
        Set(ByVal value As bilan_V2)
            _bilan = value
            refreshValeursBilan()
        End Set
    End Property
    Public Property compteBesoinsFondsPropres() As compteBesoinsFondsPropres_V2
        Get
            Return _compteBesoinsFondsPropres
        End Get
        Set(ByVal value As compteBesoinsFondsPropres_V2)
            _compteBesoinsFondsPropres = value
            refreshValeursBesoinsFondsPropres()
        End Set
    End Property
#End Region

#Region "Initialisation"

    Protected Overrides Sub initNbLignes()
        If Me.ColumnCount = 0 Then
            Me.ColumnCount = 1
        End If
        For i As Integer = 1 To 32
            Me.Rows.Add()
            Me.Rows(i - 1).Height = 15
        Next
        'Cas particuliers
        Rows(3).Height = 6
        Rows(9).Height = 6
        Rows(23).Height = 6
    End Sub
    Protected Overrides Sub initTitresBlocs()
        initLignesBlocActif()
        initLignesBlocPassif()
        initTitreFdsPropres()
        initLignesBlocCapiAlloues()
    End Sub
    Protected Overrides Sub initLignesTitresGeneraux()
        Me.Rows(0).Cells(0).Value = "BILAN"
    End Sub

    Protected Sub initTitreFdsPropres()
        'Titre des besoins en fonds propres
        Me.Rows(20).Cells(0).Value = "LE BESOIN EN FONDS PROPRES"
    End Sub
    Protected Sub initLignesBlocActif()
        'Titre du bloc
        Me.Rows(2).Cells(0).Value = "ACTIF"
        'Intitulés des comptes
        Me.Rows(4).Cells(0).Value = "Capital social non libéré"
        Me.Rows(5).Cells(0).Value = "Placements"
        Me.Rows(6).Cells(0).Value = "Total"
        'Réglage de la police
        Me.Rows(4).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(5).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub
    Protected Sub initLignesBlocPassif()
        'Titre
        Me.Rows(8).Cells(0).Value = "PASSIF"
        'Intitulés des comptes
        Me.Rows(10).Cells(0).Value = "Capitaux propres"
        Me.Rows(11).Cells(0).Value = "   Capital social ouverture"
        Me.Rows(12).Cells(0).Value = "   Apport/Retraits de capitaux (solv)"
        Me.Rows(13).Cells(0).Value = "   Résultat de l'exercice N-1"
        Me.Rows(14).Cells(0).Value = "   Apport/Retraits de capitaux (res Neg)"
        Me.Rows(15).Cells(0).Value = "Passifs subordonnés"
        Me.Rows(16).Cells(0).Value = "Provisions techniques brutes"
        Me.Rows(17).Cells(0).Value = "Total"
        'Réglage de la police d'écriture
        Me.Rows(10).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(15).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(16).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub
    Protected Sub initLignesBlocCapiAlloues()

        'Titre du compte de résultat
        Me.Rows(22).Cells(0).Value = "CAPITAUX ALLOUES"
        Me.Rows(22).Cells(0).Style = styleCellulesTitre2()
        'Intitulé des comptes
        Me.Rows(24).Cells(0).Value = "MSR"
        Me.Rows(25).Cells(0).Value = "   MSR (vie)"
        Me.Rows(26).Cells(0).Value = "   MSR (non vie)"
        Me.Rows(27).Cells(0).Value = "Capitaux alloués"
        Me.Rows(28).Cells(0).Value = "   Capitaux alloués Vie"
        Me.Rows(29).Cells(0).Value = "   Capitaux alloués Non Vie"
        Me.Rows(30).Cells(0).Value = "Capitaux alloués"

        Me.Rows(24).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(27).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub

#End Region

#Region "Dessin"
    Protected Overrides Sub paintBlocs(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        'Le bloc "Actif"
        paintBloc(e, 2, 6)
        'Le bloc "Passif"
        paintBloc(e, 8, 17)
        'Le bloc des capitaux alloués
        paintBloc(e, 22, 30)
        'Colonne de titre des besoins en fonds propres
        paintLigneTitre(e, 20)
    End Sub
    Protected Overrides Function cellFormat(ByVal rowIndex As String, ByVal columnIndex As String) As String
        If rowIndex <> 0 And columnIndex <> 0 Then Return _formatDefaut Else Return ""
    End Function
    Protected Overrides Function cellBackColor(ByVal rowIndex As String, _
                                   ByVal columnIndex As String, _
                                   ByVal ligneHautBloc As Integer, _
                                   ByVal ligneBasBloc As Integer) As System.Drawing.Color
        If rowIndex = ligneHautBloc Then
            Return Color.FromArgb(231, 213, 231)
        ElseIf rowIndex = ligneBasBloc Then
            Return Color.FromArgb(255, 230, 30)
        Else
            Return Color.White
        End If
    End Function
    Protected Overrides Function cellAlignment(ByVal rowIndex As Integer, ByVal columnIndex As Integer) As StringAlignment
        If columnIndex = 0 Then Return StringAlignment.Near Else Return StringAlignment.Far
    End Function
    Protected Overrides Function cellFont(ByVal rowIndex As Integer, ByVal columnIndex As Integer, ByVal ligneHautBloc As Integer, ByVal ligneBasBloc As Integer) As Font
        If rowIndex = ligneHautBloc Or rowIndex = ligneBasBloc Then
            Return New System.Drawing.Font("Tahoma", 9, FontStyle.Bold)
        Else
            Return New System.Drawing.Font("Tahoma", 9, FontStyle.Regular)
        End If
    End Function
#End Region

#Region "Rafraichissements demandés par les objets métiers"
    Public Overrides Function actualise(ByVal sender As Object, ByVal notification As Object) As PTIC_Util.resultatValidationContraintes
        If TypeOf sender Is compteBesoinsFondsPropres_V2 Then
            refreshValeursBesoinsFondsPropres()
        ElseIf TypeOf sender Is bilan_V2 Then
            refreshValeursBilan()
        ElseIf TypeOf sender Is ComptesMoteur2 Then
            'Actualiser depuis un changement de structure des comptes restitués
            actualiseFromComptesDeclines(sender, notification)
        End If
        Return Nothing
    End Function
#End Region

#Region "Rafraichissements élémentaires"
    Protected Overrides Sub refreshValeursBlocs()
        If (Not _bilan Is Nothing And Not _compteBesoinsFondsPropres Is Nothing) Then
            refreshValeursBesoinsFondsPropres()
            refreshValeursBilan()
        End If
    End Sub
    Protected Overrides Sub refreshTitresGeneraux()
        'Années de projection
        For j As Integer = 1 To _nbAnneesProjection
            Me.Rows(0).Cells(j).Value = CStr(j) - 1 + _anneeDepart
            Me.Rows(20).Cells(j).Value = CStr(j) - 1 + _anneeDepart
        Next
    End Sub
    Protected Function refreshValeursBesoinsFondsPropres()
        If Not compteBesoinsFondsPropres Is Nothing Then
            With compteBesoinsFondsPropres.Resultats
                If compteBesoinsFondsPropres.etatCalcul <> PTIC_Util.valueEtatCalcul.NON_INITIALISE Then
                    Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                    For i As Integer = 1 To _nbAnneesProjection
                        If Not .MSRvie Is Nothing Then Me.Rows(25).Cells(i).Value = .MSRvie(i - 1) / unite
                        If Not .MSRnonVie Is Nothing Then Me.Rows(26).Cells(i).Value = .MSRnonVie(i - 1) / unite
                        If Not .capiAllouesVie Is Nothing Then Me.Rows(28).Cells(i).Value = .capiAllouesVie(i - 1) / unite
                        If Not .capiAllouesNonVie Is Nothing Then Me.Rows(29).Cells(i).Value = .capiAllouesNonVie(i - 1) / unite
                        If Not .capiAlloues Is Nothing Then Me.Rows(30).Cells(i).Value = .capiAlloues(i - 1) / unite
                    Next
                    Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                End If
            End With
        End If
        Return Nothing
    End Function
    Protected Function refreshValeursBilan()
        If Not compteBilan Is Nothing Then
            With compteBilan
                If .etatCalcul <> PTIC_Util.valueEtatCalcul.NON_INITIALISE Then
                    Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                    For i As Integer = 1 To _nbAnneesProjection
                        With .resultats
                            If Not .capitalSocialNonLibere Is Nothing Then Me.Rows(4).Cells(i).Value = .capitalSocialNonLibere(i - 1) / unite
                            If Not .placements Is Nothing Then Me.Rows(5).Cells(i).Value = .placements(i - 1) / unite
                            If Not .totalActif Is Nothing Then Me.Rows(6).Cells(i).Value = .totalActif(i - 1) / unite
                            If Not .capitalSocialOuv Is Nothing Then Me.Rows(11).Cells(i).Value = .capitalSocialOuv(i - 1) / unite
                            If Not .apportSolvabilite Is Nothing Then Me.Rows(12).Cells(i).Value = .apportSolvabilite(i - 1) / unite
                            If Not .resultatN_1 Is Nothing Then Me.Rows(13).Cells(i).Value = .resultatN_1(i - 1) / unite
                            If Not .apportResultatNeg Is Nothing Then Me.Rows(14).Cells(i).Value = .apportResultatNeg(i - 1) / unite
                            If Not .passifsSubordonnes Is Nothing Then Me.Rows(15).Cells(i).Value = .passifsSubordonnes(i - 1) / unite
                            If Not .provisionsTechniquesBrutes Is Nothing Then Me.Rows(16).Cells(i).Value = .provisionsTechniquesBrutes(i - 1) / unite
                            If Not .totalPassif Is Nothing Then Me.Rows(17).Cells(i).Value = .totalPassif(i - 1) / unite
                        End With
                    Next
                    Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                End If
            End With
        End If
        Return Nothing
    End Function
#End Region

End Class
